<?php
/*
 * mysql_session_open()
 */
function mysql_session_open($session_path,$session_name){
	return true;
}
/*
 * mysql_session_close()
 */
function mysql_session_close(){
	return true;
}
/*
 * mysql_session_read()
 * 
 * 从数据库中读取信息
 */
function mysql_session_read($SID){
	global $cbdb;
	$value = $cbdb->get_var("SELECT value FROM {#cbdbPrefix}sessions WHERE SID='$SID' AND expiration>".time()." LIMIT 1");
	return $value?$value:'';
}
/*
 * mysql_session_write()
 * 
 * 将session数据写入数据库， 如果SID已存在，则将已有数据更新
 */
function mysql_session_write($SID,$value){
	global $cbdb;
 	$lifetime=get_cfg_var("session.gc_maxlifetime");
 	$expiration=time()+$lifetime;
 	$exist = $cbdb->get_row("SELECT value FROM {#cbdbPrefix}sessions WHERE SID='$SID' LIMIT 1");
 	if($exist){
 		$result = $cbdb->query("UPDATE `{#cbdbPrefix}sessions` SET expiration='$expiration',value='$value' WHERE  SID='$SID'");
 	}else{
 		$result = $cbdb->insert('{#cbdbPrefix}sessions',array('SID'=>$SID,'expiration'=>$expiration,'value'=>$value));
 	}
 	return $result;
}
/*
 * mysql_session_destory()
 * 
 * 删除指定会话
 */
function mysql_session_destroy($SID){
	global $cbdb;
	return $cbdb->query("DELETE FROM `{#cbdbPrefix}sessions` WHERE SID='$SID'");
}

/**
 * mysql_session_garbage_collect()
 *  
 * 垃圾回收
 * @param $lifetime
 */
function mysql_session_garbage_collect($lifetime){
	global $cbdb;
	return $cbdb->query("DELETE FROM `{#cbdbPrefix}sessions` WHERE expiration<".time());
}
